package com.lys.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lys.commonutils.R;
import com.lys.commonutils.RandomUtil;
import com.lys.exceptionHandler.GuliException;
import com.lys.staservice.client.UcenterClient;
import com.lys.staservice.entity.StatisticsDaily;
import com.lys.staservice.mapper.StatisticsDailyMapper;
import com.lys.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Lyyyys
 * @since 2021-06-27
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private StatisticsDailyMapper stamapper;

    @Autowired
    private StatisticsDailyService staService;

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        //远程调用得到某一天注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        //把获取数据添加数据库，统计分析表里面
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);//注册人数
        sta.setDateCalculated(day); //统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));

        staService.saveOrUpdate(sta);

    }

    //图表显示，返回两部分数据，日期json数组，数量json数组
    //根据前端传过来的条件值查询里面的数据
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end)  {
        //此时我要先去判断type的类型才可以决定daily用什么get方法添加进list集合中

        String methodName = "";
        switch (type){
            case "login_num":
                methodName = "getLoginNum";
                break;
            case "register_num":
                methodName = "getRegisterNum";
                break;
            case "video_view_num":
                methodName = "getVideoViewNum";
                break;
            case "course_num":
                methodName = "getCourseNum";
                break;
        }
        //根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = stamapper.selectList(wrapper);
        //因为返回的是日期和日期对应的数量
        //json两种形式，一个是对象key-value形式，一个数组形式
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个是日期list，一个是数量list
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            date_calculatedList.add(daily.getDateCalculated());
            //因为该对象是运行时才知道对象当中拿到的是哪个字段值
            //可以通过反射去调用方法
            try {
                Method method = daily.getClass().getDeclaredMethod(methodName);
                Integer result = (Integer) method.invoke(daily);
                numDataList.add(result);
            } catch (Exception e) {
                throw new GuliException(20001,"获取值失败");
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
